package de.tabuma.pm.security.controller;

import de.tabuma.pm.security.*;
import de.tabuma.lib.tabumacontroller;
import de.tabuma.ssoclient.entity.*;
import de.tabuma.ssoclient.logik.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.reflect.*;
import java.util.*;
import java.util.logging.*;
import de.tabuma.lib.contentmanagement.*;
import de.tabuma.lib.contentmanagement.entity.*;
import de.tabuma.pm.externalproject.entity.*;
import de.tabuma.pm.externalproject.logik.*;
import com.google.appengine.api.users.*;
/**
 * @author $Author: alefen $
 * @version $Revision: 1.1.1.1.6.1 $
 * @since $Date: 2010/01/13 10:19:31 $
 */
public class AttributeController extends tabumacontroller {

    private static Logger log = Logger.getLogger(GroupController.class.getName());

    public AttributeController(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }
    @Override
    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        try  {
            String securityId = request.getParameter(de.tabuma.pm.constants.SECURITY_PROVIDER_PARAM+de.tabuma.lib.contentmanagement.constants.id);
            int securityIdInt = Integer.valueOf(securityId).intValue();

            String ssoId = SecurityProviderInstanceFacade.getSingleSignOnId(securityIdInt);
            AttributeFacade af = new AttributeFacade();
            af.setSsoId(ssoId);

            String groupId = request.getParameter(de.tabuma.pm.constants.SECURITY_PROVIDER_GROUP_PARAM+de.tabuma.lib.contentmanagement.constants.id);
            int groupIdInt = Integer.valueOf(groupId).intValue();

            String userGroupId = request.getParameter(de.tabuma.pm.constants.SECURITY_PROVIDER_USER_PARAM+de.tabuma.lib.contentmanagement.constants.id);
            int userGroupIdInt = Integer.valueOf(userGroupId).intValue();
            UserGroup userGroup = UserGroupFacade.get(userGroupIdInt, ssoId);
            int userIdInt = userGroup.getUserId();

            de.tabuma.ssoclient.entity.User user = de.tabuma.ssoclient.logik.UserFacade.get(userIdInt, ssoId);

            out.println("<div class=\"contentContent\">");
            out.println("<p class=\"contentContentHeader\">Attribute Management for User "+user.getName()+"</p>");
            out.println("</div>");

            String entityName = "Attribute";

            Content content = new Content();
            Vector<Field> editableFields = new Vector<Field>();
            editableFields.add(de.tabuma.ssoclient.entity.Attribute.class.getDeclaredField("name"));
            editableFields.add(de.tabuma.ssoclient.entity.Attribute.class.getDeclaredField("value"));
            content.FormContent.setEditableFields(editableFields);

            Vector<Integer> editableTypes = new Vector<Integer>();
            editableTypes.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_LIST_STRING));
            editableTypes.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_STRING));
            content.FormContent.setEditableFieldTypes(editableTypes);

            content.FormContent.setEntityClass(de.tabuma.ssoclient.entity.Attribute.class);
            content.FormContent.setEntityName(entityName);
            
            content.FormContent.setFacadeObject(af);

            UserService userService = UserServiceFactory.getUserService();
            com.google.appengine.api.users.User user1 = userService.getCurrentUser();

            String projectId = request.getParameter(de.tabuma.pm.constants.projectId);
            int projectIdInt = Integer.valueOf(projectId);
            content.ListContent.setListParameterValue(""+userIdInt);

            content.GeneralContent.setParamName(de.tabuma.pm.constants.SECURITY_PROVIDER_ATTRIBUTE_PARAM);

            Vector<Field> listableFields = new Vector<Field>();
            listableFields.add(de.tabuma.ssoclient.entity.Attribute.class.getDeclaredField("name"));
            content.ListContent.setListableFields(listableFields);


            /*----------------------------------------------------------------------------------------------------*/
            /* Static context */
            /*----------------------------------------------------------------------------------------------------*/
            Vector<Field> staticFields = new Vector<Field>();
            staticFields.add(de.tabuma.ssoclient.entity.Attribute.class.getDeclaredField("userId"));
            content.FormContent.StaticContent.setStaticFields(staticFields);

            Vector<Integer> staticFieldsType = new Vector<Integer>();
            staticFieldsType.add(new Integer(de.tabuma.lib.contentmanagement.constants.TYPE_INTEGER));
            content.FormContent.StaticContent.setStaticFieldsTyp(staticFieldsType);

            Vector<String> staticFieldsValue = new Vector<String>();
            staticFieldsValue.add(""+userIdInt);
            content.FormContent.StaticContent.setStaticFieldsValue(staticFieldsValue);
            /*----------------------------------------------------------------------------------------------------*/

            content.GeneralContent.setServlet(this.getServlet());


            Hashtable hash = (Hashtable) this.getParameter().clone();
            hash.put(de.tabuma.pm.constants.SECURITY_PRIVIDER_ATTRIBUTE_MANAGEMENT, "true");
            hash.put(de.tabuma.pm.constants.SECURITY_PROVIDER_PARAM+de.tabuma.lib.contentmanagement.constants.id, securityId);
            content.GeneralContent.setParameter(hash);
            
            String attributeName = "Attribute Name";
            String attributeValue = "Attribute Value";

            Hashtable<Field, String> displayFields = new Hashtable<Field, String>();
            displayFields.put(de.tabuma.ssoclient.entity.Attribute.class.getDeclaredField("name"), attributeName);
            displayFields.put(de.tabuma.ssoclient.entity.Attribute.class.getDeclaredField("value"), attributeValue);
            content.FormContent.setDisplayedName(displayFields);

            List<AttributeElement> aList = new ArrayList<AttributeElement>();
            List<ExternalProject> epList = ExternalProjectFacade.getList(projectIdInt);
            for(int i=0;i<epList.size();i++) {
                ExternalProject ep = epList.get(i);
                List<LoginUserParameter> lupList = LoginUserParameterFacade.getList(ep.getId().intValue());
                for(int j=0;j<lupList.size();j++) {
                    LoginUserParameter lup = lupList.get(j);
                    AttributeElement ae = new AttributeElement();
                    ae.setId(ep.getName()+"/"+lup.getNameForUser()+" ("+lup.getId().intValue()+")");
                    ae.setName(ep.getName()+"/"+lup.getNameForUser()+" ("+lup.getId().intValue()+")");
                    aList.add(ae);
                }
            }
            log.warning("size:"+aList.size());
            Hashtable<Field, List> listParameterContent = new Hashtable<Field, List>();
            listParameterContent.put(de.tabuma.ssoclient.entity.Attribute.class.getDeclaredField("name"), aList);
            content.ListContent.setListParameterContent(listParameterContent);

            Hashtable<Field,List<Field>> listParameterContentDisplayedFields = new Hashtable<Field, List<Field>>();
            List<Field> displayFieldList = new ArrayList<Field>();
            displayFieldList.add(de.tabuma.pm.security.controller.AttributeElement.class.getDeclaredField("name"));
            listParameterContentDisplayedFields.put(de.tabuma.ssoclient.entity.Attribute.class.getDeclaredField("name"), displayFieldList);

            content.ListContent.setListParameterDisplayedField(listParameterContentDisplayedFields);
            content.GeneralContent.setUser(user1.getEmail());

            out.println(content.navigate(request));

            out.println("<div class=\"contentContent\">");
            out.println(this.getFormBeginn(null));
            out.println(this.getHiddenAsAttribute());
            out.println("<input type=\"submit\" name=\"FormBeginn\" value=\"back\" class=\"button\"/>");
            out.println(this.getFormEnd());
            out.println("</div>");
        }
        catch(Exception e) {
            String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.warning(warning);
        }
    }
}